import 'package:flutter/material.dart';

class DataTableDemo extends StatefulWidget {
  const DataTableDemo({Key? key}) : super(key: key);

  @override
  State<DataTableDemo> createState() => _DataTableDemoState();
}

class _DataTableDemoState extends State<DataTableDemo> {
  @override
  Widget build(BuildContext context) {
    return DataTable(
      sortColumnIndex: 1,
      sortAscending: true,
      showCheckboxColumn: true,
      onSelectAll: (isCheckAll) {},
      columns: [
        DataColumn(label: Text('姓名')),
        DataColumn(label: Text('年龄')),
      ],
      rows: userBeanList.map((UserBean userBean) {
        return DataRow(
            selected: userBean.isSelected,
            onSelectChanged: (value) {
              setState(() => userBean.isSelected = value!);
            },
            cells: [
              DataCell(Text(userBean.name)),
              DataCell(Text(userBean.studentId.toString())),
            ]);
      }).toList(),
    );
  }
}

class UserBean {
  String name;
  int studentId;
  bool isSelected;

  UserBean(
    this.name,
    this.studentId, {
    this.isSelected = false,
  });
}

var userBeanList = [
  UserBean('张三', 1),
  UserBean('李四', 2),
  UserBean('王二', 3),
  UserBean('麻子', 4),
  UserBean('王五', 5),
  UserBean('赵四', 6),
  UserBean('陈二', 7),
  UserBean('李世民', 8),
  UserBean('王老六', 9),
  UserBean('狗子', 10),
  UserBean('丑娃', 11),
  UserBean('石头', 12),
  UserBean('二妞', 13),
  UserBean('大妞', 14),
  UserBean('黑皮', 15),
  UserBean('大胆儿', 16),
];
